/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.view;

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.myfaces.lifecycle.LifecycleImpl;

/**
 *
 * @author chenzhuohua
 */
public class SessionLifecycleImpl extends LifecycleImpl {

    public final static String SESSION_TIMEOUT_PAGES = "/template-client.jsf";

    public SessionLifecycleImpl() {
        super();
    }

    @Override
    public void execute(FacesContext context) {
        try {
            super.execute(context);
        } catch (ViewExpiredException vee) {
            redirect(context);
        } catch (FacesException fe) {
            throw fe;
        }
    }

    private void redirect(FacesContext context) {
        try {
            context.responseComplete();
            context.renderResponse();
            HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
            String url = context.getExternalContext().getRequestContextPath() + SESSION_TIMEOUT_PAGES;
            response.sendRedirect(url);
        } catch (Exception e) {
            System.out.println(" Error: session timeout url redirect ");
        }
    }
}
